package com.system.manager.modules.projectLibrary.controller;


import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.projectLibrary.entity.Project;
import com.system.manager.modules.projectLibrary.entity.ProjectChange;
import com.system.manager.modules.projectLibrary.service.IProjectChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jisf
 * @since 2021-11-10
 */
@Api(tags = "项目变更API")
@RestController
@RequestMapping("/projectLibrary/project-change")
public class ProjectChangeController {

    @Autowired
    private IProjectChangeService iProjectChangeService;

    @ApiOperation("插入项目变更")
    @PreAuthorize(SubjectType.PROJECT_LEADER)
    @PostMapping("/insert/projectChange")
    public Result insertProjectChange(@RequestBody ProjectChange projectChange, @RequestBody Project project){
        try {
            iProjectChangeService.insertProjectChange(projectChange, project);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("审核项目变更")
    @PreAuthorize(SubjectType.zx)
    @PostMapping("/update/projectChange")
    public Result updateProjectChange(@RequestParam Integer id){
        try {
            iProjectChangeService.updateProjectChange(id);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("查询项目变更")
    @PreAuthorize({SubjectType.ENTERPRISE_ADMIN,SubjectType.PROJECT_LEADER})
    @PostMapping("/select/projectChange")
    public Result selectProjectChange(@CurrSubject Subject subject){
        try {
            List<ProjectChange> projectChanges = iProjectChangeService.selectProjectChange(subject);
            return Result.ok(projectChanges);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("删除变更记录")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @PostMapping("/delete/projectChange")
    public Result deleteProjectChange(@RequestParam Integer day, @CurrSubject Subject subject){
        try {
            iProjectChangeService.deleteProjectChange(day,subject);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
}
